<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: gavin
 * Date: 15-5-4
 * Time: 下午12:36
 */

class Xuemejson extends Foreground_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->library('form_validation');
        $this->load->helper('captcha');
        $this->load->library('pagination');
        $this->load->model('xueme_course_model');
        $this->load->model('xueme_user_model');

    }

    public function index()
    {
        $data["name"] = "xueme";
        $data['version'] = "1.0.0";
        $data['test'] = "test";
        echo json_encode($data);
    }

    public function getCourses()
    {
        $data = $this->xueme_course_model->getCourses();
        echo json_encode($data);
        //echo "OK";
    }

    public function getexample()
    {
        $data['title'] = "title is this";
        $data['name'] = "zhangsan";
        $data['phone'] = "15885266558";
        $data['address'] = "guodu";
        $data['id'] = "09527";

        echo json_encode($data);
    }


    public function signin()
    {

        //if($this->form_validation->run()  == TRUE)
        if(TRUE)
        {//通过表单验证
            $username = $this->input->get_post('username',TRUE);
            $password = $this->input->get_post('password',TRUE);

            $data = $this->xueme_user_model->existUserName($username);
            if(empty($data))
            {
                unset($data);
                $data['loginname'] = $username;
                $data['loginpasswd'] = $password;
                $this->xueme_user_model->insertValue($data);
                $result = $this->xueme_user_model->existUser($username,$password);
                $result['status'] = "success";

                echo json_encode($result);
            }
            else{//用户名重复
                unset($data);
                $data["status"] = "error";
                $data["reason"] = "username repeat";
                echo json_encode($data);
            }

        }
        else
        {//input is not legal
            echo $this->input->get("username");
            echo $this->input->get("password");
            //echo "input is not legal";
            //header("location:".SITE_URL.'index.php/admin/login');
            exit;
        }
    }


    public function test()
    {
        //$data = $this->xueme_course_model->getCourses();
        //var_dump($data);
        echo "OK";
    }
}